<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Investor
 *
 * @property integer $id
 * @property integer $user_id
 * @property string $invest_log_id
 * @property integer $org_id
 * @property integer $status
 * @property string $invest_fields
 * @property string $invest_phase
 * @property string $invest_num_small
 * @property string $invest_num_big
 * @property string $deleted_at
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property-read \App\User $toUser
 * @property-read \App\InvestOrg $toInvestOrg
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Tags[] $investor_x_tags
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\User[] $userFocusInvestor
 * @method static \Illuminate\Database\Query\Builder|\App\Investor whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Investor whereUserId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Investor whereInvestLogId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Investor whereOrgId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Investor whereStatus($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Investor whereInvestFields($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Investor whereInvestPhase($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Investor whereInvestNumSmall($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Investor whereInvestNumBig($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Investor whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Investor whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Investor whereUpdatedAt($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Fields[] $investor_x_fields
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Phases[] $investor_x_phase
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Phases[] $investor_x_phases
 */
class Investor extends Model
{

    //表名
    protected $table = "investor";
    use SoftDeletes;

    public function toUser()
    {
        return $this->belongsTo('App\User', 'user_id');
    }

    public function toInvestOrg()
    {
        return $this->belongsTo('App\InvestOrg', 'org_id');
    }

    /** 关联中间表-投资人标签 */
    public function investor_x_tags()
    {
        return $this->belongsToMany('App\Tags','tags_relations','investor_id','tag_id');
    }


    /** 关联中间表-用户关注投资人 */
    public function userFocusInvestor()
    {
        return $this->belongsToMany('App\User','focus_investor','investor_id','user_id');
    }

    /** 关联中间表-投资人投资阶段 */
    public function investor_x_phases()
    {
        return $this->belongsToMany('App\Phases','phase_relations','investor_id','phase_id');
    }

    /** 关联投资人-投资领域 */
    public function investor_x_fields()
    {
        return $this->belongsToMany('App\Fields','invest_fields','investor_id','field_id');

    }

    /**
     * 判断当前投资人是否已存在该标签
     * @param $investorId
     * @return mixed
     */
    public function isExistsTags($investorId)
    {
        return $this->investor_x_tags()->wherePivot('investor_id','=',$investorId)->exists();
    }


    /**
     * 通过项目标识 和 用户标识获取 投资时间
     * @param $projectId
     * @param $userId
     * @return mixed|string
     */
    public function getInvestTime($projectId , $userId)
    {
        $investTime = InvestLog::whereProjectId($projectId)->whereUserId($userId)->pluck('invest_time');
        if($investTime)
            return $investTime;
        else
            return '';
    }

    /**
     * 通过项目标识 用户标识获取 单个项目的投资金额
     * @param $projectId
     * @param $userId
     * @return mixed|string
     */
    public function getInvestAccount($projectId , $userId)
    {
        $investAccount = InvestLog::whereProjectId($projectId)->whereUserId($userId)->pluck('invest_account');
        if($investAccount)
            return $investAccount;
        else
            return '';
    }

    /**
     * 通过用户标识获取 该投资人所以投资的 项目总金额
     * @param $userId
     * @return int
     */
    public function getInvestAccounts($userId)
    {   $investAccounts = 0;
        if(InvestLog::whereUserId($userId)->exists())
            $investAccounts = InvestLog::whereUserId($userId)->sum('invest_account');
        return $investAccounts;
    }


}
